/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import vanScheduling.Service;

/**
 *
 * @author Dan
 */
public class ServiceDAO extends DAOTest {

    public static List<Service> getServices()
    {
        Connection con = getConnection();
        ArrayList<Service> services = new ArrayList<Service>();
        try {
            CallableStatement cs = con.prepareCall("{call stp_selAllServices(?)}");
            cs.setInt(1, 8); //hard coded now, needs to be current semester
            ResultSet rs = cs.executeQuery();

            while (rs.next())
            {
                int id = rs.getInt("activityid");
                String name = rs.getString("activity-name");
                Time startTime = rs.getTime("begin_time");
                Time endTime = rs.getTime("end_time");
                int driveTime = rs.getInt("travel_time");
                String location = rs.getString("location");
                boolean sun = rs.getBoolean("sun");
                boolean mon = rs.getBoolean("mon");
                boolean tue = rs.getBoolean("tue");
                boolean wed = rs.getBoolean("wed");
                boolean thurs = rs.getBoolean("thurs");
                boolean fri = rs.getBoolean("fri");
                boolean sat = rs.getBoolean("sat");
                Date startDate = rs.getDate("begin_date");
                Date endDate = rs.getDate("end_date");
                services.add(new Service(id, name, startTime, endTime, driveTime, location,
                        sun, mon, tue, wed, thurs, fri, sat, startDate, endDate));
            }

            cs.close();
            con.close();
        }
        catch (SQLException e) {System.err.println(e.getMessage());e.printStackTrace();}
        return services;
    }

   
    public static Service getServiceByID(int serviceID) {
        Connection con = getConnection();
        Service returnService = null;

        try {
            CallableStatement cs = con.prepareCall("{call dbo.stp_selServiceByID(?, ?)}");
            cs.setInt(1, 8); //hard coded now, needs to be current semester
            cs.setInt(2, serviceID);

            ResultSet rs = cs.executeQuery();

            while (rs.next())
            {
                int id = rs.getInt("activityid");
                String name = rs.getString("activity-name");
                Time startTime = rs.getTime("begin_time");
                Time endTime = rs.getTime("end_time");
                int driveTime = rs.getInt("travel_time");
                String location = rs.getString("location");
                boolean sun = rs.getBoolean("sun");
                boolean mon = rs.getBoolean("mon");
                boolean tue = rs.getBoolean("tue");
                boolean wed = rs.getBoolean("wed");
                boolean thurs = rs.getBoolean("thurs");
                boolean fri = rs.getBoolean("fri");
                boolean sat = rs.getBoolean("sat");
                Date startDate = rs.getDate("begin_date");
                Date endDate = rs.getDate("end_date");
                returnService = new Service(id, name, startTime, endTime, driveTime, location,
                        sun, mon, tue, wed, thurs, fri, sat, startDate, endDate);
            }

            cs.close();
            con.close();
        }
        catch (SQLException e) {e.printStackTrace();}
        return returnService;
    }

    /*
    public synchronized static boolean saveDriver(Driver driver)
    {
        Connection con = getConnection();
        boolean result = false;

        try {
            CallableStatement cs = con.prepareCall("{call dbo.stp_updDriver(?, ?, ?, ?)}");
            cs.setInt(1, driver.getId());
            cs.setString(2, driver.getName());
            cs.setString(3, driver.getEmail());
            cs.setString(4, driver.getPhoneNumber());
            int added = cs.executeUpdate();

            result = added > 0;

            cs.close();
            con.close();
        }
        catch (SQLException e) {System.err.println(e.getMessage());e.printStackTrace();}

        return result;
    }

     */
}
